package com.power.sifa.respond.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.respond.dao.Apl_respondDaoCustom;
import com.power.sifa.respond.model.Apl_respond;
import com.power.sifa.respond.model.pageInfo.Apl_respondPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 行政应诉申请 持久层实现(hibernate)
*/

@Slf4j
@Repository
public class Apl_respondDaoImpl implements Apl_respondDaoCustom {

    private final EntityManager entityManager;

    public Apl_respondDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List findList(Apl_respondPageInfo pageInfo) {
        List<Apl_respond> result = new ArrayList<>();

        StringBuffer sql = new StringBuffer("select A.*, B.applycode, B.reguserid, B.regusername, B.createtime, B.id as applyid, B" +
                ".acceptcode from  Apl_respond A " +
                " inner join apl_main B ON A.id = B.id where 1 = 1 ");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;

        // 由申请流水号调整为案号查询
        if (StringUtils.isNotEmpty(pageInfo.getF_firstcode())) {
            sql.append(" and A.firstcode like ?" + index++);
            paramsList.add("%" + pageInfo.getF_firstcode() + "%");
        }
        if (StringUtils.isNotEmpty(pageInfo.getF_plaintiffname())) {
            sql.append(" and A.plaintiffname like ?" + index++);
            paramsList.add("%" + pageInfo.getF_plaintiffname() + "%");
        }
        if (StringUtils.isNotEmpty(pageInfo.getF_regusername())) {
            sql.append(" and B.regusername like ?" + index++);
            paramsList.add("%" + pageInfo.getF_regusername() + "%");
        }
        if (ObjectUtil.isNotEmpty(pageInfo.getF_applystarttime())) {
            sql.append(" and B.createtime >= ?" + index++);
            paramsList.add(pageInfo.getF_applystarttime());
        }
        if (ObjectUtil.isNotEmpty(pageInfo.getF_applyendtime())) {
            sql.append(" and B.createtime <= ?" + index++);
            paramsList.add(pageInfo.getF_applyendtime());
        }

        // 排序
        sql.append(" order by B." + pageInfo.getSort() + " " + pageInfo.getOrder());
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());

        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        // 查询总数
        pageInfo.setAllcount(query.getResultList().size());
        // 分页
        if (pageInfo.getAllcount() > 0) {
            int pageStart = (pageInfo.getPageNumber() - 1) * pageInfo.getPageSize();
            List<Map> list = query.setFirstResult(pageStart).setMaxResults(pageInfo.getPageSize()).getResultList();
            if (CollectionUtil.isNotEmpty(list)) {
                result = JSON.parseArray(JSON.toJSONString(list), Apl_respond.class);
            }
        }
        return result;
    }
}
